
layui.use(['form', 'layer'], function () {
    let form = layui.form,
        layer = layui.layer;
    form.on('submit(submit)', function (data) {
        let _form = $(data.form);
        if (_form.length === 0) {
            layer.msg('表单未初始化完成,无法提交');
            return false;
        }
        let type = _form.attr("method") ? _form.attr("method").toUpperCase() : 'GET';
        type = type && (type === 'GET' || type === 'POST') ? type : 'GET'
        let url = _form.attr("action");
        url = url ? url : location.href;
        let loading = _form.attr('data-loading');
        loading = typeof loading == 'undefind' ? null : loading;
        let multipleList = $("[name$='[]']", _form);
        let params = {};
        if (multipleList.length > 0) {
            let postFields = _form.serializeArray().map(function (obj) {
                return $(obj).prop("name");
            });
            $.each(multipleList, function (i, j) {
                if (postFields.indexOf($(this).prop("name")) < 0) {
                    params[$(this).prop("name")] = '';
                }
            });
        }
        let formdata = _form.serialize() + (Object.keys(params).length > 0 ? '&' + $.param(params) : '');
        $.fn.myaxios({
            method: type,
            url: url,
            loading: loading,
            data: formdata,
        }, function (data, ret) {
            if (typeof success === 'function') {
                if (false === success.call(_form, data, ret)) {
                    return false;
                }
            }
        }, function (data, ret) {
            if (typeof error === 'function') {
                if (false === error.call(_form, data, ret)) {
                    return false;
                }
            }
        });
        return false;
    });
});
